<?php

namespace App\Admin\Controllers;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

use App\Models\Article;
use App\Models\ArticleCate;

class ArticleController extends AdminController
{
    protected $title = '推文';

    protected function grid()
    {
        $grid = new Grid(new Article);

        $grid->model()->with(['cate']);

        $grid->column('id', 'ID');
        $grid->column('cate.name', '分类');
        $grid->column('title', '标题');
        $grid->column('logo', '图片')->image('', 80, 80);
        $grid->column('created_at', '添加时间');
        $grid->column('updated_at', '更新时间');

        $grid->disableExport();

        return $grid;
    }

    protected function detail($id)
    {
        $show = new Show(Article::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('cate.name', '分类');
        $show->field('title', '标题');
        $show->field('logo', '图片');
        $show->field('introduction', '简介');
        $show->field('content', '内容');
        $show->field('created_at', '添加时间');
        $show->field('updated_at', '更新时间');

        return $show;
    }

    protected function form()
    {
        $form = new Form(new Article);

        $form->select('article_cate_id', '分类')->options(ArticleCate::all()->pluck('name', 'id'))->rules('required');
        $form->text('title', '标题')->rules('required');
        $form->textarea('introduction', '简介')->rows(3)->rules('required');
        $form->image('logo', '图片')->uniqueName()->rules('required');
        $form->UEditor('content', '内容')->rules('required');

        return $form;
    }
}
